/**
 * File Comment: edit toString
 */
package edu.gatech.oad.project.operations.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.gatech.oad.project.coordinator.Office;

/**
 * Medical History contains a list of 
 * appointments.
 * 
 * @author Group 17
 * @version 1.1
 */
public class MedicalHistory implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 7121689391613346371L;

    /**
     * Filed of appointments
     */
    private final List<Appointment> appointments;
    
    /**
     * Field myLogger: holds the logget for the office file
     */
    private static final Logger MYLOGGER = Logger.getLogger("edu.gatech." +
            "oad.project.operations.data");
    
    /**
	 * Default constructor
	 */
	public MedicalHistory() {
	    this.appointments = new ArrayList<Appointment>();
	    
        MYLOGGER.addHandler(Office.FILEHANDLER);
        MYLOGGER.setLevel(Level.ALL);
        MYLOGGER.finest("medical history created");
	}

	/**
	 * Returns the list of appointments
	 * 
	 * @return list of appointments
	 */
	public List<Appointment> getAppointments() {
        
        return this.appointments;
    }

    /**
     * Adds an appointment to the medical history
     * 
     * @param app the appointment to add
     */
    public void add(Appointment app) {
        this.appointments.add(app);
        MYLOGGER.info("Added an appointment to history: " + app.toString());
        
    }
    
    /**
     * This method is for use when an appointment gets cancelled
     * or a pending appointment gets rejected.
     * 
     * @param app the appointment to remove
     */
    public void removeAppointment(Appointment app){
        this.appointments.remove(app);
        MYLOGGER.info("removed an appointment from history: " + app.toString()); // $codepro.audit.disable
    } 
    
    /**
     * Returns the number of appointments in
     * medical history.
     * 
     * @return String the string representation
     */
    @Override
    public String toString(){
        final int num = this.appointments.size();
        return "There are " + Integer.toString(num)
                    + "appointments in your history.";
    }

    /**
     * Find a certain appointment.
     * 
     * @param p the appointment to find
     * @return the appointment
     */
    public Appointment find(Appointment p) {

        final int i = this.appointments.indexOf(p);
      
        if ((i >= 0) && (i < (this.appointments.size()))) {
            return this.appointments.get(i);
        }

        return null;    
    }
    
    /**
     * Finds a certain appointment by the date
     * and doctor of the appointment
     * 
     * @param p the temp appointment
     * @return the appointment
     */
    public Appointment findByDate(Appointment p) {
        Appointment temp = null;
        for (int i = 0; i < this.appointments.size(); i++) {
            temp = this.appointments.get(i);
            if ((temp.getDate() == p.getDate()) 
                    && (temp.getTime() == p.getTime())
                    && (temp.getDoc().equals(p.getDoc()))){
                return temp;
            }
        }
        
        return temp;
    }
}
